package cn.iocoder.an.module.mold.controller.admin.linestopregistration;

import cn.iocoder.an.framework.translate.core.TranslateUtils;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.linestopregistration.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.linestopregistration.LinestopRegistrationDO;
import cn.iocoder.an.module.mold.service.linestopregistration.LinestopRegistrationService;

@Tag(name = "管理后台 - 停线登记")
@RestController
@RequestMapping("/mold/linestop-registration")
@Validated
public class LinestopRegistrationController {
    @Resource
    private LinestopRegistrationService linestopRegistrationService;
    @Autowired
    private TranslateUtils translateUtils;

    @PostMapping("/create")
    @Operation(summary = "创建停线登记")
    @PreAuthorize("@ss.hasPermission('mold:linestop-registration:create')")
    public CommonResult<Long> createLinestopRegistration(@Valid @RequestBody LinestopRegistrationSaveReqVO createReqVO) {
        return success(linestopRegistrationService.createLinestopRegistration(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新停线登记")
    @PreAuthorize("@ss.hasPermission('mold:linestop-registration:update')")
    public CommonResult<Boolean> updateLinestopRegistration(@Valid @RequestBody LinestopRegistrationSaveReqVO updateReqVO) {
        linestopRegistrationService.updateLinestopRegistration(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除停线登记")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:linestop-registration:delete')")
    public CommonResult<Boolean> deleteLinestopRegistration(@RequestParam("id") Long id) {
        linestopRegistrationService.deleteLinestopRegistration(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除停线登记")
    @PreAuthorize("@ss.hasPermission('mold:linestop-registration:delete')")
    public CommonResult<Boolean> deleteLinestopRegistrationList(@RequestParam("ids") List<Long> ids) {
        linestopRegistrationService.deleteLinestopRegistrationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得停线登记")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:linestop-registration:query')")
    public CommonResult<LinestopRegistrationRespVO> getLinestopRegistration(@RequestParam("id") Long id) {
        LinestopRegistrationDO linestopRegistration = linestopRegistrationService.getLinestopRegistration(id);
        return success(BeanUtils.toBean(linestopRegistration, LinestopRegistrationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得停线登记分页")
    @PreAuthorize("@ss.hasPermission('mold:linestop-registration:query')")
    @TransMethodResult
    public CommonResult<PageResult<LinestopRegistrationRespVO>> getLinestopRegistrationPage(@Valid LinestopRegistrationPageReqVO pageReqVO) {
        PageResult<LinestopRegistrationDO> pageResult = linestopRegistrationService.getLinestopRegistrationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LinestopRegistrationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出停线登记 Excel")
    @PreAuthorize("@ss.hasPermission('mold:linestop-registration:export')")
    @ApiAccessLog(operateType = EXPORT)
    @TransMethodResult
    public void exportLinestopRegistrationExcel(@Valid LinestopRegistrationPageReqVO pageReqVO,
                                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LinestopRegistrationDO> list = linestopRegistrationService.getLinestopRegistrationPage(pageReqVO).getList();
        // 导出 Excel
/*        ExcelUtils.write(response, "停线登记.xls", "数据", LinestopRegistrationRespVO.class,
                        BeanUtils.toBean(list, LinestopRegistrationRespVO.class));*/
        ExcelUtils.write(response, "停线登记.xls", "数据", LinestopRegistrationRespVO.class,
                TranslateUtils.translate(BeanUtils.toBean(list, LinestopRegistrationRespVO.class)));
    }

}